package cn.yuanqiao.archive.appManager.service.impl;

import cn.yuanqiao.archive.appManager.domain.*;
import cn.yuanqiao.archive.appManager.mapper.TenantCategoryMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantMdataListMapper;
import cn.yuanqiao.archive.appManager.mapper.YqdaTreesMapper;
import cn.yuanqiao.archive.appManager.service.ITenantSysDictTypeService;
import cn.yuanqiao.archive.appManager.service.IYqdaTreesService;
import cn.yuanqiao.common.core.domain.TreeSelect;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * treesService业务层处理
 *
 * @author 赵常飞
 * @date 2023-06-17
 */
@Service
public class YqdaTreesServiceImpl implements IYqdaTreesService
{
    @Autowired
    private YqdaTreesMapper yqdaTreesMapper;
    @Autowired
    private TenantCategoryMapper categoryMapper;
    @Autowired
    private TenantMdataListMapper mdataListMapper;
    @Autowired
    private ITenantSysDictTypeService sysDictTypeService;

    /**
     * 查询trees
     *
     * @param Id trees主键
     * @return trees
     */
    @Override
    public YqdaTrees selectYqdaTreesById(Long Id)
    {
        YqdaTrees yqdaTrees=new YqdaTrees();
        return yqdaTreesMapper.selectYqdaTreesById(Id,yqdaTrees.getTableName());
    }

    /**
     * 查询trees列表
     *
     * @param yqdaTrees trees
     * @return trees
     */
    @Override
    public List<YqdaTrees> selectYqdaTreesList(YqdaTrees yqdaTrees)
    {
        return yqdaTreesMapper.selectYqdaTreesList(yqdaTrees,yqdaTrees.getTableName());
    }

    /**
     * 新增trees
     *
     * @param jsonObject
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertYqdaTrees(JSONObject jsonObject)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }
        String type=jsonObject.getString("type");
        String remark=jsonObject.getString("remark");
        Long parentId=jsonObject.getLong("parentId");
        Integer orderNo=jsonObject.getInteger("orderNo");
        JSONArray names = jsonObject.getJSONArray("name");
        if ("t".equals(type)|| "n".equals(type)){//树节点  空节点
            YqdaTrees yqdaTrees=new YqdaTrees();
            yqdaTrees.setParentId(parentId);
            yqdaTrees.setName(names.getString(0));
            yqdaTrees.setType(type);
            yqdaTrees.setRemark(remark);
            yqdaTrees.setOrderNo(orderNo);
            yqdaTrees.setCreator(user.getUsername());
            yqdaTrees.setCreatorId(user.getUserId());
            yqdaTrees.setCreateTime(DateUtils.getNowDate());
            yqdaTrees.setId(SnowflakeIdGenerator.generateId());
            return yqdaTreesMapper.insertYqdaTrees(yqdaTrees,yqdaTrees.getTableName());
        }else if("f".equals(type)){//全宗节点  与下面门类 分类节点代码几乎相同,避免多个if不清晰
            JSONArray fondsIds = jsonObject.getJSONArray("fondsId");
            for (int i = 0; i < names.size(); i++) {
                YqdaTrees yqdaTrees=new YqdaTrees();
                //当节点存在时,不能删除再添加,避免下面的节点丢失,能改变的只会是name
                String hasname=yqdaTreesMapper.getname(parentId," and fonds_id="+fondsIds.getLong(i),yqdaTrees.getTableName());//判断是否已经有了,有的话name是否一致
                if("".equals(hasname)||null==hasname){//无则插入
                    yqdaTrees.setId(SnowflakeIdGenerator.generateId());
                    yqdaTrees.setParentId(parentId);
                    yqdaTrees.setName(names.getString(i));
                    yqdaTrees.setFondsId(fondsIds.getLong(i));
                    yqdaTrees.setType(type);
                    yqdaTrees.setRemark(remark);
                    yqdaTrees.setOrderNo(i);
                    yqdaTrees.setCreator(user.getUsername());
                    yqdaTrees.setCreatorId(user.getUserId());
                    yqdaTrees.setCreateTime(DateUtils.getNowDate());
                    yqdaTreesMapper.insertYqdaTrees(yqdaTrees,yqdaTrees.getTableName());
                }else if(!hasname.equals(names.getString(i))){//有且不相同
                    yqdaTreesMapper.updatebypidandsql(parentId,names.getString(i)," and fonds_id="+fondsIds.getLong(i),yqdaTrees.getTableName());
                }else{//有且相同 不处理
                }
            }
            return 1;
        }else if("a".equals(type)){//门类节点
            JSONArray categoryIds = jsonObject.getJSONArray("categoryId");
            for (int i = 0; i < names.size(); i++) {
                YqdaTrees yqdaTrees=new YqdaTrees();
                //当节点存在时,不能删除再添加,避免下面的节点丢失,能改变的只会是name
                String hasname=yqdaTreesMapper.getname(parentId," and category_id="+categoryIds.getLong(i),yqdaTrees.getTableName());//判断是否已经有了,有的话name是否一致
                if("".equals(hasname)||null==hasname){//无则插入
                    yqdaTrees.setId(SnowflakeIdGenerator.generateId());
                    yqdaTrees.setParentId(parentId);
                    yqdaTrees.setName(names.getString(i));
                    yqdaTrees.setCategoryId(categoryIds.getLong(i));
                    yqdaTrees.setType(type);
                    yqdaTrees.setRemark(remark);
                    yqdaTrees.setOrderNo(i);
                    yqdaTrees.setCreator(user.getUsername());
                    yqdaTrees.setCreatorId(user.getUserId());
                    yqdaTrees.setCreateTime(DateUtils.getNowDate());
                    yqdaTreesMapper.insertYqdaTrees(yqdaTrees,yqdaTrees.getTableName());
                }else if(!hasname.equals(names.getString(i))){//有且不相同
                    yqdaTreesMapper.updatebypidandsql(parentId,names.getString(i)," and category_id="+categoryIds.getLong(i),yqdaTrees.getTableName());
                }else{//有且相同 不处理
                }
            }
            return 1;
        }else if("c".equals(type)){//分类节点
            JSONArray propertyValues = jsonObject.getJSONArray("propertyValue");
            String propertyName = jsonObject.getString("propertyName");

            //处理年度
            if("ND".equals(propertyName)){
                YqdaTrees yqdaTrees=new YqdaTrees();
                Integer startNd=Integer.parseInt(String.valueOf(names.get(0)));
                Integer lxnd=Integer.parseInt(String.valueOf(propertyValues.get(0)));
                for (int i = 0; i < lxnd; i++) {
                    String hasname=yqdaTreesMapper.getname(parentId," and property_name='"+propertyName+"' and property_value='"+(startNd+i)+"'",yqdaTrees.getTableName());//判断是否已经有了
                    if("".equals(hasname)||null==hasname) {//无则插入
                        yqdaTrees.setId(SnowflakeIdGenerator.generateId());
                        yqdaTrees.setParentId(parentId);
                        yqdaTrees.setName(String.valueOf(startNd+i));
                        yqdaTrees.setPropertyName(propertyName);
                        yqdaTrees.setPropertyValue(String.valueOf(startNd+i));
                        yqdaTrees.setType(type);
                        yqdaTrees.setRemark(remark);
                        yqdaTrees.setOrderNo(i+1);
                        yqdaTrees.setCreator(user.getUsername());
                        yqdaTrees.setCreatorId(user.getUserId());
                        yqdaTrees.setCreateTime(DateUtils.getNowDate());
                        yqdaTreesMapper.insertYqdaTrees(yqdaTrees,yqdaTrees.getTableName());
                    }else if(!hasname.equals(String.valueOf(startNd+i))){//有且不相同
                        yqdaTreesMapper.updatebypidandsql(parentId,String.valueOf(startNd+i)," and property_name='"+propertyName+"' and property_value='"+String.valueOf(startNd+i)+"'",yqdaTrees.getTableName());
                    }else{//有且相同 不处理
                    }
                }

            }else{
                for (int i = 0; i < names.size(); i++) {
                    YqdaTrees yqdaTrees=new YqdaTrees();
                    //当节点存在时,不能删除再添加,避免下面的节点丢失,能改变的只会是name
                    String hasname=yqdaTreesMapper.getname(parentId," and property_name='"+propertyName+"' and property_value='"+propertyValues.getString(i)+"'",yqdaTrees.getTableName());//判断是否已经有了,有的话name是否一致
                    if("".equals(hasname)||null==hasname){//无则插入
                        yqdaTrees.setId(SnowflakeIdGenerator.generateId());
                        yqdaTrees.setParentId(parentId);
                        yqdaTrees.setName(names.getString(i));
                        yqdaTrees.setPropertyName(propertyName);
                        yqdaTrees.setPropertyValue(propertyValues.getString(i));
                        yqdaTrees.setType(type);
                        yqdaTrees.setRemark(remark);
                        yqdaTrees.setOrderNo(i);
                        yqdaTrees.setCreator(user.getUsername());
                        yqdaTrees.setCreatorId(user.getUserId());
                        yqdaTrees.setCreateTime(DateUtils.getNowDate());
                        yqdaTreesMapper.insertYqdaTrees(yqdaTrees,yqdaTrees.getTableName());
                    }else if(!hasname.equals(names.getString(i))){//有且不相同
                        yqdaTreesMapper.updatebypidandsql(parentId,names.getString(i)," and property_name='"+propertyName+"' and property_value='"+propertyValues.getString(i)+"'",yqdaTrees.getTableName());
                    }else{//有且相同 不处理
                    }
                }
            }
            return 1;
        }else if("l".equals(type)) {//层级节点
            JSONArray subTableNames = jsonObject.getJSONArray("subTableName");
            for (int i = 0; i < names.size(); i++) {
                YqdaTrees yqdaTrees = new YqdaTrees();
                //当节点存在时,不能删除再添加,避免下面的节点丢失,能改变的只会是name
                String hasname = yqdaTreesMapper.getname(parentId, " and sub_table_name='" + subTableNames.get(i) + "'",yqdaTrees.getTableName());//判断是否已经有了,有的话name是否一致
                if ("".equals(hasname) || null == hasname) {//无则插入
                    yqdaTrees.setId(SnowflakeIdGenerator.generateId());
                    yqdaTrees.setParentId(parentId);
                    yqdaTrees.setName(names.getString(i));
                    yqdaTrees.setSubTableName(subTableNames.getString(i));
                    yqdaTrees.setType(type);
                    yqdaTrees.setRemark(remark);
                    yqdaTrees.setOrderNo(i);
                    yqdaTrees.setCreator(user.getUsername());
                    yqdaTrees.setCreatorId(user.getUserId());
                    yqdaTrees.setCreateTime(DateUtils.getNowDate());
                    yqdaTreesMapper.insertYqdaTrees(yqdaTrees,yqdaTrees.getTableName());
                } else if (!hasname.equals(names.getString(i))) {//有且不相同
                    yqdaTreesMapper.updatebypidandsql(parentId, names.getString(i), " and sub_table_name='" + subTableNames.get(i) + "'",yqdaTrees.getTableName());
                } else {//有且相同 不处理
                }
            }
            return 1;
        }
        return 0;
    }

    /**
     * 修改trees
     *
     * @param yqdaTrees trees
     * @return 结果
     */
    @Override
    public int updateYqdaTrees(YqdaTrees yqdaTrees)
    {
        return yqdaTreesMapper.updateYqdaTrees(yqdaTrees,yqdaTrees.getTableName());
    }

    /**
     * 批量删除trees
     *
     * @param Ids 需要删除的trees主键
     * @return 结果
     */
    @Override
    public int deleteYqdaTreesByIds(Long[] Ids)
    {
        YqdaTrees yqdaTrees=new YqdaTrees();
        return yqdaTreesMapper.deleteYqdaTreesByIds(Ids,yqdaTrees.getTableName());
    }

    /**
     * 删除trees信息
     *
     * @param Id trees主键
     * @return 结果
     */
    @Override
    public int deleteYqdaTreesById(Long Id)
    {
        YqdaTrees yqdaTrees=new YqdaTrees();
        return yqdaTreesMapper.deleteYqdaTreesById(Id,yqdaTrees.getTableName());
    }

    @Override
    public List<TreeSelect> getTreeList() {
        YqdaTrees yqTrees=new YqdaTrees();
        List<YqdaTrees> list = yqdaTreesMapper.getTreeListTypet(yqTrees.getTableName());
        TreeSelect rootTree = new TreeSelect();
        rootTree.setId(0L);
        rootTree.setLabel("根节点");
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(YqdaTrees yqdaTrees:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(yqdaTrees.getId());
            chilTree.setLabel(yqdaTrees.getName());
            chilTree.setNodeType(yqdaTrees.getType());
            chilTree.setOrder_no(yqdaTrees.getOrderNo());
            chilTree.setChildren(getchildrens(yqdaTrees.getId(),""));
            chilTrees.add(chilTree);
        }
        rootTree.setChildren(chilTrees);
        List<TreeSelect> treeSelects = new ArrayList<>();
        treeSelects.add(rootTree);
        return treeSelects;
    }

    private List<TreeSelect> getchildrens(Long id,String whereSql) {
        YqdaTrees yqTrees=new YqdaTrees();
        List<YqdaTrees> list = yqdaTreesMapper.getchildrens(id,yqTrees.getTableName(),whereSql);
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(YqdaTrees yqdaTrees:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(yqdaTrees.getId());
            chilTree.setLabel(yqdaTrees.getName());
            chilTree.setNodeType(yqdaTrees.getType());
            chilTree.setOrder_no(yqdaTrees.getOrderNo());
            chilTree.setCategoryId(yqdaTrees.getCategoryId());
            chilTree.setFondsId(yqdaTrees.getFondsId());
            chilTree.setPropertyName(yqdaTrees.getPropertyName());
            chilTree.setPropertyValue(yqdaTrees.getPropertyValue());
            chilTree.setSubTableName(yqdaTrees.getSubTableName());
            chilTree.setChildren(getchildrens(yqdaTrees.getId(),whereSql));
            chilTrees.add(chilTree);
        }
        return chilTrees;
    }

    @Override
    public JSONObject getfl(JSONObject json) {
        Long categoryId=json.getLong("categoryId");
        Integer level=json.getInteger("level");
        TenantCategory category=new TenantCategory();
        TenantMdataList mdataList=new TenantMdataList();
        TenantSysDictData sysDictData=new TenantSysDictData();
        String sysdictdatatableName=sysDictData.getTableName();
        if(null==level){
            level=categoryMapper.selectTenantCategoryByID(categoryId,category.getTableName()).getType();
        }
        List<Map<String,Object>> mdatalist=mdataListMapper.getdictfield(categoryId,category.getTableName(),mdataList.getTableName(),level);
        //逐个查询数据库 次数为mdatalist.size()  一次查出来 再循环 次数为mdatalist.size()*字典明细个数  数据量不大,采用逐次查询
        for (int i = 0; i < mdatalist.size(); i++) {
            Map<String,Object> mapi=mdatalist.get(i);
            List<TenantSysDictData> fieldvalues = sysDictTypeService.selectDictDataByType(String.valueOf(mapi.get("FIELD_NAME")));
            mapi.put("value",fieldvalues);
            mdatalist.set(i,mapi);
        }
        JSONObject returnjson=new JSONObject();
        returnjson.put("datalist",mdatalist);
        return returnjson;
    }

    @Override
    public List<YqdaTrees> getAllTreeList() {
        YqdaTrees yqTrees=new YqdaTrees();
        List<YqdaTrees> list = yqdaTreesMapper.getAllTreeList(yqTrees.getTableName());

        return list;
    }

    //获取角色树
    @Override
    public Map<String, Object> getroletree() {
        String tenantCode = SecurityUtils.getLoginUser().getUser().getTenantCode();
        Map returnmap=new HashMap();
        returnmap.put("id",0L);
        returnmap.put("name","角色树");
        returnmap.put("childrens",getroletreechildrenmap(tenantCode));
        return returnmap;
    }

    @Override
    public Map<String, Object> getmunutreebyrole(Long roleId) {
        Map returnmap=new HashMap();
        returnmap.put("id",0L);
        returnmap.put("name","菜单树");
        returnmap.put("childrens",getmenutreechildrenmap(roleId,0L));
        return returnmap;
    }

    @Override
    public List<TreeSelect> gettreebyrolemenuxh(Long roleId, Long menuId,String choosed) {
        YqdaTrees yqTrees=new YqdaTrees();
        List<Map<String,Object>> list = yqdaTreesMapper.getTreeListTypetrole(yqTrees.getTableName(),new YqdaTreesrole().getTableName(),roleId,menuId,choosed);
        TreeSelect rootTree = new TreeSelect();
        rootTree.setId(0L);
        rootTree.setLabel("根节点");
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(Map map:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(Long.parseLong(String.valueOf(map.get("ID"))));
            chilTree.setLabel(String.valueOf(map.get("NAME")));
            chilTree.setNodeType(String.valueOf(map.get("TYPE")));
            chilTree.setOrder_no(Integer.parseInt("null".equals(String.valueOf(map.get("ORDER_NO")))?"0":String.valueOf(map.get("ORDER_NO"))));
            chilTree.setChooesed(Integer.parseInt(String.valueOf(map.get("CHOOESED"))));
            chilTree.setChildren(getchildrensrole(Long.parseLong(String.valueOf(map.get("ID"))),roleId,menuId,choosed));
            chilTrees.add(chilTree);
        }
        rootTree.setChildren(chilTrees);
        List<TreeSelect> treeSelects = new ArrayList<>();
        treeSelects.add(rootTree);
        return treeSelects;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addtreeshouquan(JSONObject json) {
        Long roleId=json.getLong("roleId");
        Long menuId=json.getLong("menuId");
        List treeids=json.getJSONArray("treeIds");
        YqdaTreesrole treesrole=new YqdaTreesrole();
        //先删后加
        yqdaTreesMapper.deleteroletree(treesrole.getTableName(),roleId,menuId);
        for (int i = 0; i < treeids.size(); i++) {
            yqdaTreesMapper.insertroletree(treesrole.getTableName(),roleId,menuId,Long.parseLong(String.valueOf(treeids.get(i))));
        }
        return 1;
    }

    @Override
    public List<TreeSelect> gettrees(Long menuId) {
        YqdaTrees yqTrees=new YqdaTrees();
        YqdaTreesrole treesrole=new YqdaTreesrole();
        LoginUser user = SecurityUtils.getLoginUser();
        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }
        List<YqdaTrees> list = yqdaTreesMapper.gettreesgetTreeListTypet(yqTrees.getTableName(),treesrole.getTableName(),user.getUserId(),menuId);
        TreeSelect rootTree = new TreeSelect();
        rootTree.setId(0L);
        rootTree.setLabel("根节点");
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(YqdaTrees yqdaTrees:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(yqdaTrees.getId());
            chilTree.setLabel(yqdaTrees.getName());
            chilTree.setNodeType(yqdaTrees.getType());
            chilTree.setOrder_no(yqdaTrees.getOrderNo());
            chilTree.setChildren(gettreesgetchildrens(yqdaTrees.getId(),user.getUserId(),menuId,""));
            chilTrees.add(chilTree);
        }
        rootTree.setChildren(chilTrees);
        List<TreeSelect> treeSelects = new ArrayList<>();
        treeSelects.add(rootTree);
        return treeSelects;
    }

    @Override
    public List<Map<String, Object>> testsql(String sql) {
        return yqdaTreesMapper.testsql(sql);
    }

    @Override
    public void insertsql(String sql) {
        yqdaTreesMapper.insertsql(sql);
    }

    @Override
    public List<TreeSelect> getNewTreeList() {
        YqdaTrees yqTrees=new YqdaTrees();
        List<YqdaTrees> list = yqdaTreesMapper.getTreeListTypet(yqTrees.getTableName());
        TreeSelect rootTree = new TreeSelect();
        rootTree.setId(0L);
        rootTree.setLabel("根节点");
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(YqdaTrees yqdaTrees:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(yqdaTrees.getId());
            chilTree.setLabel(yqdaTrees.getName());
            chilTree.setNodeType(yqdaTrees.getType());
            chilTree.setOrder_no(yqdaTrees.getOrderNo());
            chilTree.setChildren(getchildrens(yqdaTrees.getId()," and type!='c' and  type!='l' "));
            chilTrees.add(chilTree);
        }
        rootTree.setChildren(chilTrees);
        List<TreeSelect> treeSelects = new ArrayList<>();
        treeSelects.add(rootTree);
        return treeSelects;
    }

    @Override
    public List<TreeSelect> getNewtrees(Long menuId) {
        YqdaTrees yqTrees=new YqdaTrees();
        YqdaTreesrole treesrole=new YqdaTreesrole();
        LoginUser user = SecurityUtils.getLoginUser();
        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }
        List<YqdaTrees> list = yqdaTreesMapper.gettreesgetTreeListTypet(yqTrees.getTableName(),treesrole.getTableName(),user.getUserId(),menuId);
        TreeSelect rootTree = new TreeSelect();
        rootTree.setId(0L);
        rootTree.setLabel("根节点");
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(YqdaTrees yqdaTrees:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(yqdaTrees.getId());
            chilTree.setLabel(yqdaTrees.getName());
            chilTree.setNodeType(yqdaTrees.getType());
            chilTree.setOrder_no(yqdaTrees.getOrderNo());
            chilTree.setChildren(gettreesgetchildrens(yqdaTrees.getId(),user.getUserId(),menuId," and type!='c' and  type!='l'"));
            chilTrees.add(chilTree);
        }
        rootTree.setChildren(chilTrees);
        List<TreeSelect> treeSelects = new ArrayList<>();
        treeSelects.add(rootTree);
        return treeSelects;
    }


    private List<TreeSelect> gettreesgetchildrens(Long id, Long userid,Long menuId,String whereSql) {
        YqdaTrees yqTrees=new YqdaTrees();
        YqdaTreesrole treesrole=new YqdaTreesrole();
        List<YqdaTrees> list = yqdaTreesMapper.getchildrensbyrole(yqTrees.getTableName(),treesrole.getTableName(),id,userid,menuId,whereSql);
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(YqdaTrees yqdaTrees:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(yqdaTrees.getId());
            chilTree.setLabel(yqdaTrees.getName());
            chilTree.setNodeType(yqdaTrees.getType());
            chilTree.setOrder_no(yqdaTrees.getOrderNo());
            chilTree.setCategoryId(yqdaTrees.getCategoryId());
            chilTree.setFondsId(yqdaTrees.getFondsId());
            chilTree.setPropertyName(yqdaTrees.getPropertyName());
            chilTree.setPropertyValue(yqdaTrees.getPropertyValue());
            chilTree.setSubTableName(yqdaTrees.getSubTableName());
            chilTree.setChildren(gettreesgetchildrens(yqdaTrees.getId(),userid,menuId,whereSql));
            chilTrees.add(chilTree);
        }
        return chilTrees;
    }

    private List<TreeSelect> getchildrensrole(Long treeid, Long roleId, Long menuId,String choosedsql) {
        YqdaTrees yqTrees=new YqdaTrees();
        List<Map<String,Object>> list = yqdaTreesMapper.getchildrensrole(treeid,yqTrees.getTableName(),new YqdaTreesrole().getTableName(),roleId,menuId,choosedsql);
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(Map map:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(Long.parseLong(String.valueOf(map.get("ID"))));
            chilTree.setLabel(String.valueOf(map.get("NAME")));
            chilTree.setNodeType(String.valueOf(map.get("TYPE")));
            chilTree.setOrder_no(Integer.parseInt("null".equals(String.valueOf(map.get("ORDER_NO")))?"1":String.valueOf(map.get("ORDER_NO"))));
            if("null".equals(String.valueOf(map.get("CATEGORY_ID")))){
            }else{
                chilTree.setCategoryId(Long.parseLong(String.valueOf(map.get("CATEGORY_ID"))));
            }
            chilTree.setChooesed(Integer.parseInt(String.valueOf(map.get("CHOOESED"))));
            if("null".equals(String.valueOf(map.get("FONDS_ID")))){
            }else{
                chilTree.setCategoryId(Long.parseLong(String.valueOf(map.get("FONDS_ID"))));
            }
            //chilTree.setFondsId(Long.parseLong(String.valueOf(map.get("FONDS_ID"))));
            chilTree.setPropertyName(String.valueOf(map.get("PROPERTY_NAME")));
            chilTree.setPropertyValue(String.valueOf(map.get("PROPERTY_VALUE")));
            chilTree.setSubTableName(String.valueOf(map.get("SUB_TABLE_NAME")));
            chilTree.setChildren(getchildrensrole(Long.parseLong(String.valueOf(map.get("ID"))),roleId,menuId,choosedsql));
            chilTrees.add(chilTree);
        }
        return chilTrees;
    }

    private List<Map<String, Object>> getmenutreechildrenmap(Long roleId,Long superId) {
        List<Map<String, Object>> list=yqdaTreesMapper.getmenutreechildrenmap(roleId,superId);
        List<Map<String, Object>> returnlist=new ArrayList<>(list.size());
        for (int i = 0; i <list.size(); i++) {
            Map<String, Object> returnmap=new HashMap<>(1<<2);
            returnmap.put("id",list.get(i).get("MENU_ID"));
            returnmap.put("name",list.get(i).get("MENU_NAME"));
            returnmap.put("childrens",(getmenutreechildrenmap(roleId,(Long)list.get(i).get("TEMPLATE_ID"))));
            returnlist.add(returnmap);
        }
        return returnlist;
    }


    private List<Map<String, Object>> getroletreechildrenmap(String tenantCode) {
        List<Map<String, Object>> list=yqdaTreesMapper.getroletreechildrenmap(tenantCode);
        List<Map<String, Object>> returnlist=new ArrayList<>(list.size());
        for (int i = 0; i <list.size(); i++) {
            Map<String, Object> returnmap=new HashMap<>(1<<2);
            returnmap.put("id",list.get(i).get("ROLE_ID"));
            returnmap.put("name",list.get(i).get("ROLE_NAME"));
            returnmap.put("childrens",new ArrayList<>(0));
            returnlist.add(returnmap);
        }
        return returnlist;
    }


}
